'use strict';
module.exports = {
	/**
	 * 此函数名称
	 * @url client/user/kh/test 前端调用的url参数地址
	 * data 请求参数
	 * @params {String} params1  参数1
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, originalParam } = event;
		let { customUtil, config, pubFun, vk, db, _ } = util;
		let { uid, inviteCode } = data;
    console.log(data)
    debugger
		let res = { code : 0, msg : '' };
		// 业务逻辑开始-----------------------------------------------------------
		// 可写与数据库的交互逻辑等等
    let dbName = 'uni-id-users'
    let curUser = await vk.baseDao.findById({
      dbName,
      id: uid
    });
    if (!curUser) {
      res.msg = '用户不存在'
      return res
    }
    if (curUser.inviter_uid) {
      res.msg = '用户已被其他人邀请'
      return res
    }
    if (curUser.my_invite_code === inviteCode) {
      res.msg = '自己不能邀请自己'
      return res
    }
    
    let inviterUser = await vk.baseDao.findByWhereJson({
      dbName,
      whereJson:{
        my_invite_code: inviteCode
      }
    });
    if (!inviterUser || inviterUser.status !== 0) {
      res.msg = '邀请用户不存在'
      return res
    }
    
   let f = await vk.baseDao.updateById({
      dbName,
      id: uid,
      dataJson:{
        inviter_uid: inviterUser._id,
        invite_time: Date.now()
      },
       getUpdateData:true
    });
    console.log(f)
    res.msg = '邀请成功'
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}
